package com.photologics.main;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.InputFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.photologics.logic.LogicHelper;

public class ShopItemSelectDialog extends Dialog implements OnClickListener {

	ImageButton ok, cancle;
	Context mcontext;
	EditText itemCheck;
	TextView sumItem;
	ImageView itemView;
	int num = 1;
	String selectItme;

	public ShopItemSelectDialog(Context context, String item, int coin) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

		setContentView(R.layout.shop_itme_select_dialog);
		selectItme = item;
		mcontext = context;
		ok = (ImageButton) findViewById(R.id.button_item_ok);
		cancle = (ImageButton) findViewById(R.id.button_itme_cancle);

		sumItem = (TextView) findViewById(R.id.textview_sumitem);
		itemView = (ImageView) findViewById(R.id.item_select_view);

		ok.setOnClickListener(this);
		cancle.setOnClickListener(this);

		if (selectItme.equals("itemHorizontal")) {
			itemView.setImageResource(R.drawable.item_horizontal);
		} else if (selectItme.equals("itemVertical")) {
			itemView.setImageResource(R.drawable.item_vertical);
		} else if (selectItme.equals("itemSquar")) {
			itemView.setImageResource(R.drawable.item_squar);
		} else if (selectItme.equals("itemShowImage")) {
			itemView.setImageResource(R.drawable.item_showimage);
		} else if (selectItme.equals("itemRemoveX")) {
			itemView.setImageResource(R.drawable.item_removex);
		}

	}

	@Override
	public void onClick(View v) {
		if (v == ok) {

			itemCheck = (EditText) findViewById(R.id.EditText_item_check);
			itemCheck.setSingleLine();
			int maxLength = 4;
			InputFilter[] filterArray = new InputFilter[1];
			filterArray[0] = new InputFilter.LengthFilter(maxLength);
			itemCheck.setFilters(filterArray);

			if (itemCheck.getText().toString().equals("")) {
				num = 1;
			} else
				num = Integer.parseInt(itemCheck.getText().toString());

			if (selectItme.equals("itemHorizontal")) {

				if (LogicHelper.accountInfo.getCoin() < 500 * num) {
					Toast.makeText(mcontext, "코인이 모자랍니다", Toast.LENGTH_SHORT)
							.show();
				} else {
					LogicHelper.accountInfo.changeCoin(-500 * num);
					LogicHelper.accountInfo.changeHorizontal(num);
				}

				ShopDialog.countItemHorizontal.setText("보유개수 :"
						+ LogicHelper.accountInfo.getHorizontal());

			} else if (selectItme.equals("itemVertical")) {

				if (LogicHelper.accountInfo.getCoin() < 500 * num) {
					Toast.makeText(mcontext, "코인이 모자랍니다", Toast.LENGTH_SHORT)
							.show();
				} else {

					LogicHelper.accountInfo.changeCoin(-500 * num);
					LogicHelper.accountInfo.changeVertical(num);
				}
				ShopDialog.countItemVertical.setText("보유개수 :"
						+ LogicHelper.accountInfo.getVertical());
			} else if (selectItme.equals("itemSquar")) {

				if (LogicHelper.accountInfo.getCoin() < 1000 * num) {
					Toast.makeText(mcontext, "코인이 모자랍니다", Toast.LENGTH_SHORT)
							.show();
				} else {

					LogicHelper.accountInfo.changeCoin(-1000 * num);
					LogicHelper.accountInfo.changeSquar(num);
				}
				ShopDialog.countItemSquar.setText("보유개수 :"
						+ LogicHelper.accountInfo.getSquar());
			} else if (selectItme.equals("itemShowImage")) {
				if (LogicHelper.accountInfo.getCoin() < 1500 * num) {
					Toast.makeText(mcontext, "코인이 모자랍니다", Toast.LENGTH_SHORT)
							.show();
				} else {

					LogicHelper.accountInfo.changeCoin(-1500 * num);
					LogicHelper.accountInfo.changeShowImage(num);
				}
				ShopDialog.countItemShowImage.setText("보유개수 :"
						+ LogicHelper.accountInfo.getShowImage());
			} else if (selectItme.equals("itemRemoveX")) {

				if (LogicHelper.accountInfo.getCoin() < 2000 * num) {
					Toast.makeText(mcontext, "코인이 모자랍니다", Toast.LENGTH_SHORT)
							.show();
				} else {

					LogicHelper.accountInfo.changeCoin(-2000 * num);
					LogicHelper.accountInfo.changeRemoveX(num);
				}
				ShopDialog.countItemRemoveX.setText("보유개수 :"
						+ LogicHelper.accountInfo.getRemoveX());
			}

			ShopDialog.countCoin.setText(LogicHelper.accountInfo.getCoin()
					+ " ");

			dismiss();
		} else if (v == cancle) {
			dismiss();
		}

	}
}